﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;

namespace ART.Common
{
    public class JTree
    {
        public static DataTable GetTreeTable(DataTable dt, string pid, string name)
        {
            return GetTreeTable(0, dt, pid, "0", name);
        }
        public static DataTable GetTreeTable(DataTable dt, string pid, string pidvalue, string name)
        {
            return GetTreeTable(0, dt, pid, pidvalue, name);
        }
        /// <summary>
        /// 返回列表页用的DataTable(列表页用)
        /// </summary>
        /// <param name="DataTable">DataTable</param>
        /// <returns>DataTable</returns>
        public static DataTable GetTreeTable(int Top, DataTable dt, string pid, string pidvalue, string name)
        {
            DataTable newdt = dt.Clone();
            DataRow[] dt_Parent = dt.Select(pid + "="+pidvalue);
            foreach (DataRow dr in dt_Parent)
            {
                dr[name] = "<span style='Color:#ff0000'>├ " + dr[name].ToString() + "</span>";
                string ID = dr["ID"].ToString();
                newdt.Rows.Add(dr.ItemArray);
                Recursion_GetTreeTable(Top, ref newdt, dt, ID, "　", pid, name);
            }
            return newdt;
        }
        //GetTreeTable递归函数
        private static void Recursion_GetTreeTable(int Top, ref DataTable newdt, DataTable dt, string ID, string fontstring, string pid, string name)
        {
            DataRow[] dt_Child = dt.Select(pid + "=" + ID, "SORT");

            foreach (DataRow dr in dt_Child)
            {
                string Gname = fontstring + dr[name].ToString();
                string G1 = null;//空格
                string G2 = null;//字符
                for (int j = 0; j < Gname.Length; j++)
                {
                    if (Gname[j].ToString() == "　")
                    {
                        G1 = G1 + Gname[j];
                    }
                    else
                    {
                        G2 = G2 + Gname[j];
                    }

                }
                //G1 = G1 + "├" + " ";
                dr[name] = System.Web.HttpUtility.HtmlDecode(G1 + G2);
                string ID_Child = dr["ID"].ToString();
                newdt.Rows.Add(dr.ItemArray);
                string FontString = fontstring + "　"; //　
                if (Top == 0 || fontstring.Length < Top)
                {
                    Recursion_GetTreeTable(Top, ref newdt, dt, ID_Child, FontString, pid, name);
                }
            }
        }
        public static List<ListItem> GetClassTree(DataTable dt, string pid, string name)
        {
            return GetClassTree(0, dt, pid, "0", name);
        }
        /// <summary>
        /// 根据Type返回List(DropDownList)
        /// </summary>
        /// <param name="Type">Type</param>
        /// <returns>List</returns>
        public static List<ListItem> GetClassTree(DataTable dt, string pid, string pidvalue, string name)
        {
            return GetClassTree(0, dt, pid, pidvalue, name);
        }
        /// <summary>
        /// 根据Type返回List(DropDownList)
        /// </summary>
        /// <param name="Type">Type</param>
        /// <returns>List</returns>
        public static List<ListItem> GetClassTree(int Top, DataTable dt, string pid, string pidvalue, string name)
        {
            List<ListItem> list = new List<ListItem>();
            DataRow[] dt_Parent = dt.Select(pid + "=" + pidvalue);
            foreach (DataRow dr in dt_Parent)
            {
                string Name = "├ " + dr[name].ToString();
                string ID = dr["ID"].ToString();
                list.Add(new ListItem(Name, ID));
                Recursion_GetClassTree(Top, ref list, dt, ID, "　", pid, name);
            }
            return list;
        }
        //GetClassTree递归函数
        private static void Recursion_GetClassTree(int Top, ref List<ListItem> list, DataTable dt, string ID, string fontstring, string pid, string name)
        {
            DataRow[] dt_Child = dt.Select(pid+"=" + ID);

            foreach (DataRow dr in dt_Child)
            {
                string Gname = fontstring + dr[name].ToString();
                string G1 = null;
                string G2 = null;
                for (int j = 0; j < Gname.Length; j++)
                {
                    if (Gname[j].ToString() == "　")
                    {
                        G1 = G1 + Gname[j];
                    }
                    else
                    {
                        G2 = G2 + Gname[j];
                    }

                }
                G1 = G1 + "├" + " ";
                string Name_Child = System.Web.HttpUtility.HtmlDecode(G1 + G2);
                string ID_Child = dr["ID"].ToString();
                list.Add(new ListItem(Name_Child, ID_Child));
                string FontString = fontstring + "　";
                if (Top==0||fontstring.Length < Top)
                {
                    Recursion_GetClassTree(Top, ref list, dt, ID_Child, FontString, pid, name);
                }
            }
        }


    }
}
